package view;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class Principal extends JFrame {
    private JPanel jpMenu;
    private JPanel jpAreaAplicacao;
    private JButton jbCadastros, jbConsultas, jbPacientes, jbRelatorios;
    private Container conteudo;
    private Inicial inicial;
    private Pacientes pacientes;
    private Consultas consultas;
    private Cadastros cadastros;
    private Relatorios relatorios;

    public Principal(){
        super("Saúde & Cia. - Gestão de Clínicas");
        
        //Instânciar objetos
        conteudo = getContentPane();
        jpAreaAplicacao = new JPanel(new CardLayout());
        jpMenu = new JPanel();
        inicial = new Inicial();
        pacientes = new Pacientes();
        consultas = new Consultas();
        cadastros = new Cadastros();
        relatorios = new Relatorios();
        jbCadastros = new JButton("Cadastros", new ImageIcon("src\\media\\enfermeira.png"));
        jbConsultas = new JButton("Consultas", new ImageIcon("src\\media\\seringa.png"));
        jbPacientes = new JButton("Pacientes", new ImageIcon("src\\media\\ambulancia.png"));
        jbRelatorios = new JButton("Relatórios", new ImageIcon("src\\media\\medico.png"));
        
        //definir propriedades visuais
        jpMenu.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jpMenu.setBackground(Color.WHITE);
        setarAlinhamentoTextoBotao(jbCadastros);
        setarAlinhamentoTextoBotao(jbConsultas);
        setarAlinhamentoTextoBotao(jbPacientes);
        setarAlinhamentoTextoBotao(jbRelatorios);
        
        //definir layouts
        conteudo.setLayout(new BorderLayout());
        jpMenu.setLayout(new BoxLayout(jpMenu, BoxLayout.Y_AXIS));
        
        //definir listeners
        jbConsultas.addActionListener(new TratarBotoesMenu());
        jbPacientes.addActionListener(new TratarBotoesMenu());
        jbCadastros.addActionListener(new TratarBotoesMenu());
        jbRelatorios.addActionListener(new TratarBotoesMenu());
        
        //adicionar objetos a conteineres        
        conteudo.add(jpAreaAplicacao, BorderLayout.CENTER);
        jpAreaAplicacao.add(inicial, "inicial");
        jpAreaAplicacao.add(pacientes, "pacientes");
        jpAreaAplicacao.add(consultas, "consultas");
        jpAreaAplicacao.add(cadastros, "cadastros");
        jpAreaAplicacao.add(relatorios, "relatorios");
        jpMenu.add(jbPacientes);
        jpMenu.add(util.ElementoVazio.gerar(0, 5));
        jpMenu.add(jbConsultas);
        jpMenu.add(util.ElementoVazio.gerar(0, 5));
        jpMenu.add(jbCadastros);
        jpMenu.add(util.ElementoVazio.gerar(0, 5));
        jpMenu.add(jbRelatorios);
        conteudo.add(jpMenu, BorderLayout.LINE_START);
        
        //setar propriedades da tela e mostra-la
        setSize(1000, 700);
        setLocationRelativeTo(null);
        setVisible(true);
    }

    private void setarAlinhamentoTextoBotao(JButton botao) {
        botao.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);  
        botao.setVerticalAlignment(javax.swing.SwingConstants.TOP);  
        botao.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
    }
    
    private class TratarBotoesMenu implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
            CardLayout cardLayout = (CardLayout) (jpAreaAplicacao.getLayout());
            if(e.getSource() == jbPacientes){
                cardLayout.show(jpAreaAplicacao, "pacientes");
            } else if (e.getSource() == jbConsultas){
                cardLayout.show(jpAreaAplicacao, "consultas");
            } else if(e.getSource() == jbCadastros) {
                cardLayout.show(jpAreaAplicacao, "cadastros");
            } else if(e.getSource() == jbRelatorios) {
                cardLayout.show(jpAreaAplicacao, "relatorios");
            }
        }
    }
    
    public static void main(String [] args){
        Principal p = new Principal();
        p.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
